package com.whmnx.main.ui.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.whmnx.base.adapter.GirdDropDownAdpter;
import com.whmnx.base.bean.OrderBean;
import com.whmnx.base.bean.ShopListBean;
import com.whmnx.base.bean.ShopShowBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.router.RouterActivityPath;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.base.view.RoundImageView;
import com.whmnx.main.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * author : huang
 * date   : 2020/10/26 0026 15:28
 * version: 1.0
 * @author Administrator
 */


public class ShowShopAdapter extends CommonRecyclerAdapter<ShopShowBean> {

    private Activity mContext;

    private int count = 1;
    private int limitCount = 1;
    private String productSPEC_name;
    private double productSPEC_price;
    private boolean flag = false;
    private double productSPEC_groupPrice;

    public ShowShopAdapter(Context context) {
        super(context, R.layout.item_show_shop);
            this.mContext = (Activity) context;

    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, ShopShowBean item, int position) {
        helper.setText(R.id.tv_num,position + 1 + "");
        helper.setText(R.id.tv_goods_desc,item.getProduct_Name());
        helper.setText(R.id.tv_moeny,"￥" + item.getProduct_Price());
        helper.setText(R.id.tv_desc,item.getProduct_SPEC());
        if (item.getProduct_Type() == 0){
            helper.setText(R.id.tv_shang,"加入购物车");
            helper.setBackgroundRes(R.id.tv_shang,R.drawable.red_drawable_oval2);
        }else {
            helper.setText(R.id.tv_shang,"我要开团");
            helper.setBackgroundRes(R.id.tv_shang,R.drawable.yellow_drawable_oval);
        }
        if (!TextUtils.isEmpty(item.getProduct_Image())){
            if (item.getProduct_Image().contains(",")){
                ImageUtil.setImage(helper.getView(R.id.iv_goods), item.getProduct_Image().split(",")[0]);
            }else {
                ImageUtil.setImage(helper.getView(R.id.iv_goods), item.getProduct_Image());
            }
        }

        //商品详情
        helper.getView(R.id.rl).setOnClickListener(v -> {
            SPUtils.getInstance().put("Product_Id", item.getProduct_Id());
            ARouter.getInstance().build(RouterActivityPath.Main.ORDER).navigation();
           // RxBus.getDefault().post("Product_Id—" + item.getProduct_Id());
        });

        helper.getView(R.id.tv_shang).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialogHead = new Dialog(mContext, R.style.ActionSheetDialogStyle);
                View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_video2, null);

                RoundImageView ivImage = view.findViewById(R.id.iv_image);
                TextView tvName = view.findViewById(R.id.tv_Name);
                TextView tvPrice = view.findViewById(R.id.tv_price);
                TextView tvCountTitle = view.findViewById(R.id.tv_count_title);
                TextView tvJian = view.findViewById(R.id.tv_jian);
                TextView tvJia = view.findViewById(R.id.tv_jia);
                TextView tvCount = view.findViewById(R.id.tv_count);
                TextView tvAdd = view.findViewById(R.id.tv_add);
                LinearLayout rlShop = view.findViewById(R.id.rl_shop);
                LinearLayout rlOpen = view.findViewById(R.id.rl_open);
                TextView tvPeNum = view.findViewById(R.id.tv_peopleNum);
                TextView tv1 = view.findViewById(R.id.tv1);
                TextView tv2 = view.findViewById(R.id.tv2);
                TextView tv3 = view.findViewById(R.id.tv3);

                if (item.getProduct_Type() == 0) {
                    rlShop.setVisibility(View.GONE);
                    tv3.setText("确认");
                    tv2.setVisibility(View.GONE);
                } else {
                    tvPeNum.setVisibility(View.VISIBLE);
                    tvPeNum.setText(item.getPeopleNum() + "人拼团");
                    //开团价格
                    tv2.setText("￥" + item.getProduct_Price());
                    if (item.getSPECModel().size() > 0)
                    tv1.setText("￥" + item.getSPECModel().get(0).getProductSPEC_Price());
                }


                GridView gv = view.findViewById(com.whmnx.base.R.id.gv);

                if (item.getProduct_Image().contains(",")) {
                    ImageUtil.setImage(ivImage, item.getProduct_Image().split(",")[0]);
                } else {
                    ImageUtil.setImage(ivImage, item.getProduct_Image());
                }

                tvName.setText(item.getProduct_Name());
                tvPrice.setText("￥" + item.getProduct_Price());
                tvCountTitle.setText("数量: " + 1);

                view.findViewById(R.id.iv_close).setOnClickListener(v1 -> dialogHead.dismiss());
                //-
                tvJian.setOnClickListener(v12 -> {
                    if (count > limitCount) {
                        count -= limitCount;
                        tvCountTitle.setText("数量: " + count);
                        tvCount.setText(String.valueOf(count));
                    }
                });
                //+
                tvJia.setOnClickListener(v12 -> {
                    count += limitCount;
                    tvCountTitle.setText("数量: " + count);
                    tvCount.setText(String.valueOf(count));
                });

                rlShop.setOnClickListener(v13 -> {
                    if (!flag) {
                        ToastUtils.showShort("请先选择规格");
                        return;
                    }
                    //拼团
                    List<ShopListBean> shopListBean = new ArrayList<>();
                    List<ShopListBean.ProductListBean> productListBean = new ArrayList<>();
                    productListBean.add(new ShopListBean.ProductListBean());
                    productListBean.get(0).setProduct_Id(item.getProduct_Id());
                    productListBean.get(0).setProduct_Name(item.getProduct_Name());
                    productListBean.get(0).setProduct_Image(item.getProduct_Image());
                    productListBean.get(0).setProduct_SPEC(productSPEC_name);
                    productListBean.get(0).setProduct_Price(productSPEC_price + "");
                    productListBean.get(0).setProduct_Num(count);
                    shopListBean.add(new ShopListBean());

                    shopListBean.get(0).setProductList(productListBean);
                    shopListBean.get(0).setMId(item.getUserInfo_Id());

                    Map<String, Object> params = new HashMap<>();
                    params.put("shopList", shopListBean);
                    params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
                    params.put("isGroup", 0);
                    HttpClient.post(mContext, Constants.GETORDERINFO, params, new CallBack<OrderBean>() {
                        @Override
                        public void onSuccess(OrderBean result) {
                            Constants.ORDERBEAN = result;
                            ARouter.getInstance().build(RouterActivityPath.Home.COM).navigation();
                        }
                    });
                });

                rlOpen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!flag) {
                            ToastUtils.showShort("请先选择规格");
                            return;
                        }
                        if (item.getProduct_Type() == 0) {  //单独买
                            Map<String, Object> params = new HashMap<>();
                            params.put("shopCart_ProductId", item.getProduct_Id());
                            params.put("shopCart_ProductImage", item.getProduct_Image());
                            params.put("shopCart_ProductName", item.getProduct_Name());
                            params.put("shopCart_SPECName", productSPEC_name);
                            params.put("shopCart_SPECMoney", productSPEC_price);
                            params.put("shopCart_Num", count);
                            params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));

                            HttpClient.post(mContext, Constants.ADDSHOPCART, params, new CallBack<OrderBean>() {
                                @Override
                                public void onSuccess(OrderBean result) {
                                    ToastUtils.showShort("加入购物车成功");
                                    dialogHead.dismiss();
                                }
                            });
                        } else {  //我要开团
                            Map<String, Object> params = new HashMap<>();
                            params.put("orderGroup_No", "");
                            params.put("orderGroup_ProductId", item.getProduct_Id());
                            params.put("orderGroup_ProductName", item.getProduct_Name());
                            params.put("orderGroup_ProductImage", item.getProduct_Image());
                            params.put("orderGroup_ProductSPEC", productSPEC_name);
                            params.put("orderGroup_ProductPrice", productSPEC_groupPrice);
                            params.put("orderGroup_ProductNum", count);
                            params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
                            HttpClient.post(mContext, Constants.SPELLGROUP, params, new CallBack<OrderBean>() {
                                @Override
                                public void onSuccess(OrderBean result) {
                                    Constants.ORDERBEAN = result;
                                    ARouter.getInstance().build(RouterActivityPath.Home.COM).navigation();
                                }
                            });
                        }
                    }
                });


                dialogHead.setContentView(view);
                Window window = dialogHead.getWindow();
                if (window != null) {//宽高最大
                    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//显示对话框//设置Dialog从窗体底部弹出
                    dialogHead.setCanceledOnTouchOutside(true);
                    dialogHead.show();
                    window.setGravity(Gravity.BOTTOM);
                    WindowManager.LayoutParams lp = window.getAttributes();//设置Dialog距离底部的距离
                    //将属性设置给窗体
                    window.setAttributes(lp);
                    GirdDropDownAdpter cityAdapter = new GirdDropDownAdpter(mContext, item.getSPECModel());
                    gv.setAdapter(cityAdapter);
                    cityAdapter.setCheck(0);
                    gv.setOnItemClickListener((parent, view1, position1, id) -> {
                        flag = true;
                        Button tv = view1.findViewById(R.id.tv);
                        if (tv.isSelected() == false) {
                            if (item.getSPECModel().size() > 0) {
                                productSPEC_name = item.getSPECModel().get(position1).getProductSPEC_Name();
                                productSPEC_price = item.getSPECModel().get(position1).getProductSPEC_Price();
                                productSPEC_groupPrice = item.getSPECModel().get(position1).getProductSPEC_GroupPrice();
                            }
                            tv.setSelected(true);
                            if (item.getProduct_Type() == 1) {
                                if (item.getSPECModel().size() > 0) {
                                    tv1.setText("￥" + productSPEC_price);
                                    tv2.setText("￥" + item.getSPECModel().get(position1).getProductSPEC_GroupPrice());
                                    tvPrice.setText("￥" + item.getSPECModel().get(position1).getProductSPEC_GroupPrice());
                                }
                            } else {
                                tvPrice.setText("￥" + productSPEC_price);
                            }
                        } else {
                            tv.setSelected(false);
                        }
                    });
                }

            }
        });
    }
}
